package com.jrx.anytxn.reporting.job.loanAcct.config;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.BtBatchStatus;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.bean.ReportingDataBean;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.loanAcct.step.AutoAssemReportingProcessor;
import com.jrx.anytxn.reporting.job.loanAcct.step.AutoAssemReportingReader;
import com.jrx.anytxn.reporting.job.loanAcct.step.AutoAssemReportingWriter;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class AutoAssemReportingConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(AutoAssemReportingConfiguration.class);


    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private AutoAssemReportingProcessor autoAssemReportingProcessor;

    @Autowired
    private AutoAssemReportingWriter autoAssemReportingWriter;

    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;

    @Bean
    public Job autoAssemReportingJob(){
      return jobBuilderFactory.get("autoAssemReportingJob")
              .start(autoAssemReportingJobStep())
              .build();
    }

    @Bean
    public Step autoAssemReportingJobStep(){
         return stepBuilderFactory.get("autoAssemReportingJobStep")
                .<AmLoanAccountInfo, ReportingDataBean>chunk(anyTxnBatchProperties.getReportingLoanAcctConfig().getChunkLimit())
                .reader(autoAssemReportingReader())
                .processor(autoAssemReportingProcessor)
                .writer(autoAssemReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingLoanAcctConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<AmLoanAccountInfo> autoAssemReportingReader(){
        logger.info("抽取订单信息批量启动,机构号：{}",ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        BtBatchStatus statuses = extBtBatchStatusMapper.selectByStatusAndJobName(ReportingConstant.BATCH_SATTUS_2,
                ReportingConstant.CUTOVER_END,org.getTenantId(),org.getToday());
        String startTime = null;
        if(null != statuses){
            startTime = DateUtils.format(statuses.getCreateTime(),"yyyy-MM-dd HH:mm:ss");
        }
        return new AutoAssemReportingReader(sqlSessionFactory,anyTxnBatchProperties.getReportingLoanAcctConfig().getPageSize(),startTime);
    }

}
